<template>
  <el-container class="rootbox">
    <el-main style="    padding: 0px;overflow-x: hidden">
      <el-alert style="padding: 3px 0px;"
          effect="dark"
        title="选择多行进行打印，或者点击某行右侧按钮进行打印。"
        type="warning"
        :closable="false"
        show-icon>
      </el-alert>
      <el-row :gutter="20" style="padding: 9px 0px;">
        <el-col :span="8">
          已选打印服务器:{{printer_label_server_name}}
        </el-col>
      </el-row>
      <div>
        <data-view
          ref="dataview"
          propViewKey="wm_stock_snprint"
          :propAutoFetchData="false"
          :propPageSize="isByWin() ? -1: null"
          :propHidePagination="isByWin()"
          :propShowToolBar="true"
          :propSubHeight="120"
          class="data-view-bar"
          @extra-button-click="handleExtraButton"
          @after-load="afterLoad"
          :propDisableCheck="propDisableCheck"
        />
      </div>
    </el-main>
  </el-container>
</template>
<script>
/*eslint-disable*/

export default {
  data() {
    return {
      printParams: { //请求打印的参数
       label: {
        action: 'initLabel',
        app_key: 'SPD_3.0',
        code: 'PrintSN',
        version: '1',
        type: 'NASO'
       }
      },
      printPara: {}, //打印的参数
      txtScan: null,
      printerSelId: null,
      printer: {},
      printers: null,
      propDisableCheck: `(row.rfid !=null && row.rfid.length >4)`,
      printer_label_server_name: '未设置',
    }
  },
  methods: {
    //搜索表格-外部
    /**
     * @description: 搜索表格-外部
     * @author: Tai
     * @date: 2024/9/13 16:07
     * @param propLineId        原始入库明细id
     * @param propWorkOrderKey  入库单号
     * @param propSnArr         sn列表
     **/
    gridSearchOut(propLineId, propWorkOrderKey, propSnArr) {
      let wrapper = this.$newWrapper()
      if (propLineId) {
        wrapper.eq('lineId', propLineId)
      } else  if (propWorkOrderKey) {
        wrapper.eq('workOrderKey', propWorkOrderKey)
      }else  if (propSnArr) {
        wrapper.in('sn', propSnArr)
      }
      this.$refs.dataview.setQueryWrapper(wrapper)
    },
    isByWin() {
      return !(!this.propLineId && !this.propWorkOrderKey &&  !this.propSnArr)
    },
    //按钮事件
    handleExtraButton(para) {
      //行打印或者表头打印
      if (para.button.key == 'printSNLine' || para.button.key == 'printSN') {
        let rows=[];
        if(para.button.key == 'printSN'){
          rows = this.$refs.dataview.$refs.tbMain.getCheckboxRecords()
        }else{
          rows.push(para.data)
        }
        if (!rows || rows.length <1) {
          this.utilMessage.warning('请选择需要打印的记录')
          return
        }

        // 打印次数限制
        if (this.parameters.isPrintOnlyOne() && this.propPrintOnlyOne) {
          let snArrErr = rows.filter(tmp => tmp.printNum && tmp.printNum >= 1).map(temp2 => {
            return temp2.sn
          })
          if (snArrErr.length > 0) {
            this.utilMessage.warning(`以下标签已打印，请勿重复打印，如需重打，请使用标签补打功能。 ${snArrErr.join(",")}`)
            return
          }

        }
        // this.$set(this.printParams, 'data', rows)
        // axios.post('/print/PrintWorker/MasoPrint/PrintByTemplet', this.printParams).then(resp => {
        // })
        // if(!chrome || !chrome.webview) {
        //   return
        // }
        // return chrome.webview.postMessage(para)

        let  printer_label_serverid = this.utils.loadUserState(this,'printer_label_serverid')
        let  printer_label_server_type_key = this.utils.loadUserState(this,'printer_label_server_type_key')


        //标签打印机名称
        let printerLabelConfig = this.utils.loadPrinterLabelConfig(this)
        if (!printerLabelConfig || !printerLabelConfig.printerServerType) {
          this.utilMessage.warning('请先到工作台设置标签打印服务器')
          return
        }

        if (printerLabelConfig.printerServerType == 'rfid') { //RFID打印机
          this.utilVue.loadShow(true)
          let params={
            snArr: [],
            printerId: printer_label_serverid

          }
          rows.forEach(ob => {params.snArr.push(ob.sn)})
          this.$ajax.post('/tool/printRFID',params, {timeout: 1000 * 60 * 10}).then(resp => {
            if (resp.code == 0) {
              this.utilMessage.success(resp.msg)
            }
            this.utilVue.loadHide()
            this.$refs.dataview.ocRefresh()
          })

        } else if (printerLabelConfig.printerServerType == 'label') {//普通标签打印机
          let param = {
            rows: rows
          }
          global.utilPrint.openLabel(param, () => {
            this.$refs.dataview.ocRefresh()
          })
        }
      } else if (para.button.key == 'close') {
        this.$emit('close-barcode')
      }
    },


    afterLoad(){
      if (this.propLineId || this.propWorkOrderKey ||  this.propSnArr)  {
        //选中
        this.$refs.dataview.$refs.tbMain.setAllCheckboxRow(true)
      }else { //隐藏关闭按钮
        this.$refs.dataview.setButtonInvisible('view', 'close', true)
      }
    },

    //设置打印机
    setPrint() {
      //标签打印机名称
      let printerLabelConfig = this.utils.loadPrinterLabelConfig(this)
      if (printerLabelConfig) {
        this.printer_label_server_name = printerLabelConfig.printerServerName
      }
    }
  },
  props: {
    //入库单明细ID筛选
    propLineId: {String, default: null},
    //入库单号
    propWorkOrderKey: {String, default: null},
    //要打印的sn列表
    propSnArr: {String, default: null},
    //是否只允许打印一次
    propPrintOnlyOne: {Boolean, default: false}
  },
  mounted() {
  },
  watch: {
    // 用户状态改变，更新打印设置
    '$store.state.users.userState': {
      immediate: true, // 初始创建 watch 时立即执行回调函数
      deep: true, // 监听到对象里面的值的变化
      handler() {
        this.setPrint()
      },
    },
  }
}
</script>

<style lang="scss" scoped>
  .rootbox{
    height: 100%;
    padding: 5px;
  }
</style>
